<?php
/**
 * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说/图/谱/源/码/分/享
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/12 0012
 * Time: 15:55
 */
defined("IN_IA")or exit("Access Denied");
checklogin();  //验证是否登录
global $_W,$_GPC;
$uniacid=$_W['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] : "index";
if($op=='index'){
	$condition=array();
	    if(!empty($_GPC['nickname'])){
	        $nickname=trim($_GPC['nickname']);
	        $condition['nickname LIKE']= '%'.$nickname.'%';
            $list=pdo_getall("cqkundian_farm_user",$condition,'','','create_time desc');
        }else{
            $condition['uniacid']=$uniacid;
            $listCount=pdo_getall("cqkundian_farm_user",$condition);
            $total=count($listCount);   //数据的总条数
            $pageSize=10; //每页显示的数据条数
            $pageIndex=intval($_GPC['page']) ? intval($_GPC['page']) :1;  //当前页
            $pager=pagination($total,$pageIndex,$pageSize);
            $list=pdo_getall("cqkundian_farm_user",$condition,'','','create_time desc',array($pageIndex,$pageSize));
        }

        for ($i=0;$i<count($list);$i++){
            //查询该用户的优惠券
            $userCoupon=pdo_getall('cqkundian_farm_user_coupon',array('uniacid'=>$uniacid,'uid'=>$list[$i]['uid']));
            $list[$i]['coupon_count']=count($userCoupon);
        }

	    include $this->template("web/user/index");
}

if($op=='edit'){
	$edit_where=array(
        'id'=>$_GPC['id'],
        'uniacid'=>$uniacid,
    );
    $list=pdo_get("cqkundian_farm_user",$edit_where);
	include $this->template("web/user/edit");
}

if($op=='saveModel'){
	$updateData=array(
        'nickname'=>$_GPC['nickname'],
        'avatarurl'=>tomedia($_GPC['avatarurl']),
        'phone'=>$_GPC['phone'],
        'address'=>$_GPC['address'],
        'uniacid'=>$uniacid,
        'create_time'=>time(),
    );

    if(empty($_GPC['id'])){
        $request=pdo_insert("cqkundian_farm_user",$updateData);
    }else{
        $model_where=array(
            'id'=>$_GPC['id'],
            'uniacid'=>$uniacid,
        );
        $request=pdo_update("cqkundian_farm_user",$updateData,$model_where);
    }
    if($request){
        message('操作成功',$this->createWebUrl('user'));
    }else{
        message("操作失败");
    }
}

if($op=='delete'){
	$condition=array();
    $condition['id']=$_GPC['id'];
    $condition['uniacid']=$uniacid;
    $request=pdo_delete("cqkundian_farm_user",$condition);
    if($request){
        echo  json_encode(array('status'=>1,'msg'=>"操作成功"));
    }else{
        echo json_encode(array('status'=>2,'msg'=>"操作失败"));
    }
}

//查询用户拥有的优惠券数量
if($op=='coupon_list'){
    $uid=$_GPC['uid'];
    $user=pdo_get('cqkundian_farm_user',array('uniacid'=>$uniacid,'uid'=>$uid));
    $listCount=pdo_getall("cqkundian_farm_user_coupon",array('uniacid'=>$uniacid,'uid'=>$uid));
    $total=count($listCount);   //数据的总条数
    $pageSize=15; //每页显示的数据条数
    $pageIndex=intval($_GPC['page']) ? intval($_GPC['page']) :1;  //当前页
    $pager=pagination($total,$pageIndex,$pageSize);
    $userCoupon=pdo_getall('cqkundian_farm_user_coupon',array('uniacid'=>$uniacid,'uid'=>$uid),'','','create_time desc',array($pageIndex,$pageSize));
    for ($i=0;$i<count($userCoupon);$i++){
        $coupon = pdo_get('cqkundian_farm_shop_coupon', array('uniacid' => $uniacid, 'id' => $userCoupon[$i]['cid']));
        if($userCoupon[$i]['status']==1){
            $userCoupon[$i]['state']=1;  //已使用
        }else{
            //优惠券是否过期
            if ($coupon['expiry_date'] == 1) {
                $coupon['expiry_time']=date("Y-m-d", ($userCoupon[$i]['create_time'] + ($coupon['expiry_day'] * 86400)));
                if (($userCoupon[$i]['create_time'] + ($coupon['expiry_day'] * 86400)) > time()) { //未过期
                    $userCoupon[$i]['state']=0;
                }else{
                    $userCoupon[$i]['state']=2;  //已过期
                }
            } else {
                if (time() < $coupon['end_time']) {
                    $userCoupon[$i]['state']=0;
                }else{
                    $userCoupon[$i]['state']=2;
                }
            }
        }
        if($coupon['expiry_date']==2){
            $coupon['begin_time'] = date("Y-m-d", $coupon['begin_time']);
            $coupon['end_time'] = date("Y-m-d", $coupon['end_time']);
        }else{
            $coupon['expiry_time']=date("Y-m-d", ($userCoupon[$i]['create_time'] + ($coupon['expiry_day'] * 86400)));
        }
        if($coupon['type']==1){
            $coupon['type_chinese_name']='普通商城可用';
        }elseif ($coupon['type']==2){
            $coupon['type_chinese_name']='组团商城可用';
        }elseif ($coupon['type']==3){
            $coupon['type_chinese_name']='畜牧领养可用';
        }elseif ($coupon['type']==4){
            $coupon['type_chinese_name']='租地可用';
        }elseif ($coupon['type']==5){
            $coupon['type_chinese_name']='种子购买可用';
        }
        $userCoupon[$i]['coupon']=$coupon;
        $userCoupon[$i]['create_time']=date("Y-m-d",$userCoupon[$i]['create_time']);
    }

    include $this->template("web/user/coupon_list");
}

//删除用户的优惠券
if($op=='delete_coupon'){
    $res=pdo_delete('cqkundian_farm_user_coupon',array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
    if($res){
        echo json_encode(array('status'=>1));die;
    }else{
        echo json_encode(array('status'=>2));die;
    }
}

//用户积分记录
if($op=='score_record'){
    $uid=$_GPC['uid'];
    $all=pdo_getall('cqkundian_farm_integral_record',array('uniacid'=>$uniacid,'uid'=>$uid));
    $count=count($all);
    $pageIndex=$_GPC['page'] ? $_GPC['page'] :1;
    $pager=pagination($count,$pageIndex,15);
    $list=pdo_getall('cqkundian_farm_integral_record',array('uniacid'=>$uniacid,'uid'=>$uid),'','','id desc',array($pageIndex,15));
    $user=pdo_get('cqkundian_farm_user',array('uniacid'=>$uniacid,'uid'=>$uid));
    include $this->template("web/user/score_record");
}

//删除积分记录
if($op=='score_record_del'){
    $res=pdo_delete('cqkundian_farm_integral_record',array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
    if($res){
        echo json_encode(array('status'=>1));die;
    }else{
        echo json_encode(array('status'=>2));die;
    }
}

